part of pda.page;

class PageCheckItem extends StatefulWidget {
  final CheckBill bill;

  PageCheckItem(this.bill);

  @override
  State<StatefulWidget> createState() {
    return _PageCheckItemState();
  }
}

class _PageCheckItemState extends State<PageCheckItem> {
  TextEditingController searchController = TextEditingController();
  FocusNode searchFocusNode = FocusNode();
  FocusNode qtyFocusNode = FocusNode();
  String searchBy;
  final Map<String, String> searchHint = {
    'barcode': '按条码搜索',
    'name': '按名称搜索',
    'spec': '按规格搜索',
    'spell': '按拼音码搜索',
  };

  @override
  void initState() {
    super.initState();
    searchBy = 'barcode';
  }

  @override
  Widget build(BuildContext context) {
    FocusScope.of(context).requestFocus(searchFocusNode);
    return WillPopScope(
      child: Scaffold(
          appBar: AppBar(
            title: Text('明细编辑'),
          ),
          body: Column(
            children: <Widget>[
              Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(right: 8),
                  ),
                  DropdownButtonHideUnderline(
                    child: DropdownButton(
                      isDense: false,
                      //减少按钮的高度。默认情况下，此按钮的高度与其菜单项的高度相同。如果isDense为true，则按钮的高度减少约一半。 这个当按钮嵌入添加的容器中时，非常有用
                      iconSize: 32.0,
                      isExpanded: false,
                      value: searchBy,
                      items: [
                        DropdownMenuItem(
                          child: Text('条码'),
                          value: 'barcode',
                        ),
                        DropdownMenuItem(
                          child: Text('名称'),
                          value: 'name',
                        ),
                        DropdownMenuItem(
                          child: Text('规格'),
                          value: 'spec',
                        ),
                        DropdownMenuItem(
                          child: Text('拼音码'),
                          value: 'spell',
                        ),
                      ],
                      onChanged: (value) {
                        setState(() {
                          searchBy = value;
                        });
                      },
                    ),
                  ),
                  Expanded(
                    child: TextField(
                        focusNode: searchFocusNode,
                        controller: searchController,
                        textInputAction: TextInputAction.search,
                        decoration: InputDecoration(
//                contentPadding: EdgeInsets.all(0),
                            hintText: searchHint[searchBy],
                            suffixIcon: IconButton(
                              icon: Icon(Icons.remove_circle),
                              onPressed: () {
                                searchController.clear();
                              },
                            )),
                        onEditingComplete: () {
//                          debugPrint('onEditingComplete');
                          search();
                        }),
                  ),
//        Container(padding: EdgeInsets.only(left: 8.0), child: Text('用户:')),
                  IconButton(
                    padding: EdgeInsets.only(top: 8.0),
                    icon: Icon(Icons.search),
                    onPressed: () {
                      search();
                    },
                  )
                ],
              ),
              Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8.0),
                    child: Text('名称：'),
                  ),
                  Expanded(
                    child: TextField(
                      enabled: false,
                    ),
                  ),
                  Padding(padding: EdgeInsets.symmetric(horizontal: 8.0)),
                ],
              ),
              Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8.0),
                    child: Text('规格：'),
                  ),
                  Expanded(
                    child: TextField(
                      enabled: false,
                    ),
                  ),
                  Padding(padding: EdgeInsets.symmetric(horizontal: 8.0)),
                ],
              ),
              Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8.0),
                    child: Text('条码：'),
                  ),
                  Expanded(
                    child: TextField(
                      enabled: false,
                    ),
                  ),
                  Padding(padding: EdgeInsets.symmetric(horizontal: 8.0)),
                ],
              ),
              Row(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.symmetric(horizontal: 8.0),
                    child: Text('数量：'),
                  ),
                  Expanded(
                    child: TextField(
                      focusNode: qtyFocusNode,
                      keyboardType: TextInputType.number,
                      inputFormatters: [_DecimalInputFormatter()],
//                      enabled: false,
                    ),
                  ),
                  Padding(padding: EdgeInsets.symmetric(horizontal: 8.0)),
                ],
              ),
            ],
          )),
      onWillPop: () async {
        return true;
      },
    );
  }

//  TextInputFormatter decimalInputLimit() {
//    return TextInputFormatter.w;
//  }

  search() {
    debugPrint('search:${searchController.text}');
    var key = searchController.text;
    if (key.isEmpty) {
      showAlert(context, '请输入搜索关键字');
      FocusScope.of(context).requestFocus(searchFocusNode);
      return;
    }
    Product.query(searchBy, '%$key%').then((list) {
      if (list == null || list.isEmpty) {
        showAlert(context, '搜索结果为空');
        return;
      }
      showModalBottomSheet(context: context, builder: (context) {

      });
    }).catchError((_) {
      showError(context, _.toString());
    });
  }

  Widget productsSheet(var list) {}
}

class _DecimalInputFormatter extends service.TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
      TextEditingValue oldValue, TextEditingValue newValue) {
    var value = double.tryParse(newValue.text);
    return value == null ? oldValue : newValue;
  }
}
